package com.spyx.activity.controller;


import com.atyx.spyx.model.activity.CouponInfo;
import com.atyx.spyx.vo.activity.CouponRuleVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spyx.activity.service.CouponInfoService;
import com.spyx.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 优惠券信息 前端控制器
 * </p>
 *
 * @author yhp
 * @since 2024-05-21
 */
@RestController
@RequestMapping("/admin/activity/couponInfo")
//@CrossOrigin
@Api(tags = "优惠券")
public class CouponInfoController {

    @Autowired
    private CouponInfoService couponInfoService;

    @ApiOperation("优惠券分页")
    @GetMapping("{page}/{limit}")
    public Result getList(@PathVariable("page")Integer page,
                          @PathVariable("limit")Integer limit){
        //1.封装分页数据
        Page p1 = new Page<>(page,limit);
        //2.调取service层方法
        IPage<CouponInfo> pageModel=couponInfoService.selectList(p1);
        return Result.ok(pageModel);
    }

    @ApiOperation("主键查询")
    @GetMapping("get/{id}")
    public Result get(@PathVariable("id") Integer id){
        CouponInfo couponInfo = couponInfoService.getById(id);
        //设置类型
        couponInfo.setCouponTypeString(couponInfo.getCouponType().getComment());
        return Result.ok(couponInfo);
    }


    @ApiOperation("新增")
    @PostMapping("/save")
    public Result save(@RequestBody CouponInfo couponInfo){
        couponInfoService.save(couponInfo);
        return Result.ok();
    }

    @ApiOperation("修改")
    @PutMapping("/update")
    public Result update(@RequestBody CouponInfo couponInfo){
        couponInfoService.updateById(couponInfo);
        return Result.ok();
    }

    @ApiOperation("删除")
    @DeleteMapping("/remove/{id}")
    public Result delete(@PathVariable("id")Integer rid){
        couponInfoService.removeById(rid);
        return Result.ok();
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList){
        couponInfoService.removeByIds(idList);
        return Result.ok();
    }

    //根据优惠券id,查询优惠券使用规则
    @ApiOperation("根据优惠券id,查询优惠券使用规则")
    @GetMapping("/findCouponRuleList/{id}")
    public Result findCouponRuleList(@PathVariable("id")Integer id){
        Map<String,Object> map=couponInfoService.findCouponRuleList(id);
        return Result.ok(map);
    }

    //保存优惠券使用规则
    @ApiOperation("保存优惠券使用规则")
    @PostMapping("saveCouponRule")
    public Result saverule(@RequestBody CouponRuleVo couponRuleVo){
        couponInfoService.saveRule(couponRuleVo);
        return Result.ok();
    }

    //优惠券模糊查
    @GetMapping("findCouponByKeyword/{keyword}")
    @ApiOperation("优惠券模糊查")
    public Result findCouponByKeyword(@PathVariable("keyword")String keyword){
      List<CouponInfo>  list=  couponInfoService.findCouponByKeyword(keyword);
        return Result.ok(list);
    }
}

